package com.weilai.common.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.weilai.common.model.DrawRecord;
import com.weilai.common.model.RedeemCode;
import com.weilai.common.model.SysUser;
import com.weilai.common.service.DrawRecordService;
import com.weilai.common.utils.StringRandom;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by Song on 2017/6/4.
 */
@Controller
@RequestMapping("/drawRecord")
public class DrawRecordController {
//    该记录后面有个核销按钮，当该中奖记录被兑奖时，点击核销，
// 该记录状态变更为已核销，同样的，用户中奖纪录里面的状态也会相应的变化，
// 变为已兑奖。 避免重复兑奖问题

    @Autowired
    DrawRecordService drawRecordService;

    /**
     * 修改兑奖状态
     * @param model
     * @param drawRecord
     * @return
     */
    @RequestMapping("/updateState")
    public String  updateState(Model model, @ModelAttribute DrawRecord drawRecord,@RequestParam Integer id){
        drawRecord.setId(id);
        //httpcode 界面用的状态码  200代表成功   400 代表失败  （假的）
        if (drawRecordService.updateState(drawRecord)== 1){
            model.addAttribute("httpCode","200");
            model.addAttribute("code", StringRandom.getStringRandom(8));
        }else{
            model.addAttribute("httpCode","400");
        }
        return "redirect:/drawRecord/prizeRecord";
    }

    /**
     * 显示所有用户的中奖记录，显示中奖微信号，中奖奖品。能够通过微信号查找该记录
     * @param model
     * @return
     */
    @RequestMapping("prizeRecord")
    public String findAll(Model model,@RequestParam(defaultValue = "1",required = false) int pageNum,
                          @RequestParam(defaultValue = "10",required = false) int pageSize){

        //赋值页面页码大小自动帮我们计算，我们只需要在页面上使用PageInfo的数据就行
        PageHelper.startPage(pageNum, pageSize);
        Page<DrawRecord> redeemCodeList = (Page) drawRecordService.findUserOfPrize(new DrawRecord());
        //分页信息
        PageInfo<DrawRecord> pageInfo = new PageInfo(redeemCodeList);
        model.addAttribute("page",pageInfo);

//        List<DrawRecord> userOfPrize = drawRecordService.findUserOfPrize(drawRecord);
//        model.addAttribute("userOfPrize",userOfPrize);
//        for (DrawRecord dr:userOfPrize) {
////            System.out.println("userOfPrize====////////////////=============================="+dr.toString());
//            System.out.println("userOfPrize====////////////////=============================="+dr.getCashPrizeState());
//        }
        return "houtai/zhongjiang";
    }

    /**
     * 用户查找的兑奖记录
     * @param request
     * @return
     */
    @RequestMapping(value = "findUserOfPrize",method = {RequestMethod.POST})
    @ResponseBody
    public List<DrawRecord> findUserOfPrize( HttpServletRequest request){
        SysUser user = (SysUser)request.getSession().getAttribute("user");
        DrawRecord drawRecord = new DrawRecord();
//        SysUser user = new SysUser();
//        user.setId(2);
        drawRecord.setUser(user);
        List<DrawRecord> userOfPrize = drawRecordService.findUserOfPrize(drawRecord);
//        model.addAttribute("userOfPrize",userOfPrize);
        return userOfPrize;
    }

    /**
     * 通过微信号查找中奖纪录

     * @param model
     * @return
     */
    @RequestMapping("/findByWeiXin")
    public String findByWeiXin(@RequestParam String weixinNumber,Model model,@RequestParam(defaultValue = "1",required = false) int pageNum,
                               @RequestParam(defaultValue = "10",required = false) int pageSize) {

        //赋值页面页码大小自动帮我们计算，我们只需要在页面上使用PageInfo的数据就行
        PageHelper.startPage(pageNum, pageSize);
        Page<RedeemCode> byWeiXin = (Page) drawRecordService.findByWeiXin(weixinNumber);
        //分页信息
        PageInfo<RedeemCode> pageInfo = new PageInfo(byWeiXin);
        model.addAttribute("page",pageInfo);

//        List<DrawRecord> byWeiXin = drawRecordService.findByWeiXin(weixinNumber);
//        //分页信息
//
//        model.addAttribute("userOfPrize", byWeiXin);
//        for (DrawRecord dr : byWeiXin) {
////            System.out.println("userOfPrize====////////////////=============================="+dr.toString());
//            System.out.println("userOfPrize====79797979797979=============================" + dr.getCashPrizeState());
//        }
            return "houtai/zhongjiang";
        }

    }

